/*
 * @(#)XFileRenderer.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.util;

import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.io.File;

/**
 * Clase que obtiene el comoponente asociado a un fichero en funci�n
 * de la lista en la que se vaya a mostrar
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XFileRenderer implements ListCellRenderer {

	//----------------------------------------------------------------------------//
	//                           CONSTANTES P�BLICAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la lista desplegable superior de la ventana de selecci�n
	 * de ficheros
	 */
	public static final int DIRECTORY = 1;
	
	/**
	 * C�digo asociado a la lista central de la ventana de selecci�n de ficheros
	 */
	public static final int FILE = 2;
	
	/**
	 * C�digo asociado a la lista desplegable del campo filtro en la ventana de
	 * selecci�n de ficheros
	 */
	public static final int FILTER = 3;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Tipo de lista
	 */
	private int code;
	
	/**
	 * Referencia a la ventana de selecci�n de ficheros
	 */
	private XFileChooser chooser;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XFileRenderer(int code, XFileChooser chooser) {
		this.code = code;
		this.chooser = chooser;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Interfaz ListCellRenderer
	 */
	public Component getListCellRendererComponent(
			JList list, Object value, int index,
			boolean isSelected, boolean cellHasFocus) {

		if(code == DIRECTORY) return getDirectoryComponent(value);
		if(code == FILE) return getFileComponent(value, isSelected);
		if(code == FILTER) return getFilterComponent(value);
		return null;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Componente correspondiente al desplegable de directorios
	 */
	private Component getDirectoryComponent(Object value) {
		File file = (File) value;
		XIndentIcon ii = new XIndentIcon(file, chooser.getIcon(file));

		boolean root = FileSystemView.getFileSystemView().isRoot(file);
		String label = "";
		if(file == null) label = "null";
		else if(root) label = file.getAbsolutePath(); 
		else label = file.getName();
		JLabel renderer = new JLabel(label);
		renderer.setIcon(ii);
		renderer.setOpaque(true);
		renderer.setForeground(Color.black);
		renderer.setBackground(XConstants.textbackground);
		renderer.setFont(XConstants.textfont);
		return renderer;
	}

	/**
	 * Componente correspondiente a la lista de ficheros
	 */
	private Component getFileComponent(Object value, boolean isSelected) {
		File file = (File) value;
		boolean root = FileSystemView.getFileSystemView().isRoot(file);
		String label = root ? file.getAbsolutePath() : file.getName();
		JLabel renderer = new JLabel(label);
		renderer.setIcon(chooser.getIcon(file));
		renderer.setOpaque(true);
		renderer.setBackground(isSelected? XConstants.textselectedbg:
			XConstants.textbackground);
		renderer.setForeground(Color.black);
		renderer.setFont(XConstants.textfont);
		return renderer;
	}

	/**
	 * Componente correspondiente a la lista de filtros
	 */
	private Component getFilterComponent(Object value) {
		FileFilter filter = (FileFilter) value;
		JLabel renderer = new JLabel(filter.getDescription());
		renderer.setOpaque(true);
		renderer.setForeground(Color.black);
		renderer.setBackground(XConstants.textbackground);
		renderer.setFont(XConstants.textfont);
		return renderer;
	}
}

